package jco3.sessionManagement_JCoContext;

import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;

public class TestSessionSameThread {

	/**
	 * 
	 * @param args
	 * @throws JCoException
	 * @throws InterruptedException
	 * 
	 * @description runtime在execute执行之后，会重设会话状态（session state)，
	 * 将连接(connetion)归还给连接池(connetion pool)。这样的机制，在对用户会话（user sesseion）有要求的时候，
	 * 就出问题了涉及到俩个Function的调用的时候，如果第一个函数执行之后，runtime把连接(connection)归还给了
	 * 连接池(connection pool)，后面函数的提交就不会成功。
	 */
	public static void main(String[] args) throws JCoException,InterruptedException{
		// get JCoDestination object instance
		JCoDestination destination = JCoDestinationManager.getDestination("ECC");
		
		// make sure the two functions will be executed in the same session
		JCoContext.begin(destination);
		
		// Before increment
		System.out.println("Before execution of ZINCREMENT_COUNTER:");
        System.out.println("Counter:" + RfcFunctions.runGetCounter(destination));
        
        // Run incrementCounter five times
        for(int i=0;i<5;i++) {
        	RfcFunctions.runIncrement(destination);
        	System.out.println("Add:" + (i + 1));
        }
        
        // After increment  
        System.out.println("After execution of ZINCREMENT_COUNTER:");
        System.out.println("Counter:" + RfcFunctions.runGetCounter(destination));    
        
        // release the connection 
        JCoContext.end(destination);
	}
}
